﻿CREATE PROCEDURE [dbo].[Chapter_GetItem]
	@hashTag NVARCHAR(128) = NULL
	,	@userId UNIQUEIDENTIFIER = NULL
	, @copyShare BIT = NULL
	, @shares NVARCHAR(MAX) = NULL
AS
BEGIN
	DECLARE @chapterId UNIQUEIDENTIFIER, @bookId UNIQUEIDENTIFIER

	--SELECT @chapterId = [ChapterId], @bookId = [BookId]
	--FROM [dbo].[text_Chapter]
	--WHERE LOWER([HashTag]) = LOWER(@hashTag) AND [ParentChapterId] IS NOT NULL AND ([IsPublished] = 1 OR [dbo].[IsViewUnpublished](@userId) = 1)

	--IF @chapterId IS NULL
	--	RAISERROR ('UnknownUrl', 16, 1)

	--SELECT @chapterId AS [ChapterId]

	--------------------- BOOK INFO ------------------------
	--SELECT b.[BookId]
	--			,	[ChapterId]
	--			,	[HashTag]
	--			,	[IsPublished]
	--			,	[Title]
	--			,	[Author]
	--			,	[CoverUrl]
	--FROM [dbo].[text_Chapter] AS c
	--INNER JOIN [dbo].[text_Book] AS b on c.[BookId] = b.[BookId]
	--WHERE b.[BookId] = @bookId
	--------------------- BOOK INFO ------------------------
	--------------------- CHAPTERS INFO ------------------------
	--SELECT [ChapterId]
	--			,[ParentChapterId]
	--			,[BookId]
	--			,[HashTag]
	--			,[IsPublished]
	--			, /*CASE WHEN [Cost] IS NULL
	--				THEN NULL
	--				ELSE
	--					[dbo].[GetChapterCost]([ChapterId], @userId, [Cost])
	--				END*/ NULL AS [Cost]
	--			,[Order]
	--			,[Title]
	--			,[Hint]
	--			,[UpdateDate]
	--FROM [dbo].[text_Chapter]
	--WHERE [BookId] = @bookId
	--------------------- CHAPTERS INFO ------------------------

	--------------------- CHAPTER TEXT ------------------------

	--IF @copyShare = 1
	--BEGIN
	--	DECLARE @CURSOR CURSOR
	--	DECLARE @CURRENTSHARE NVARCHAR(MAX)

	--	SET @CURSOR  = CURSOR SCROLL
	--	FOR
	--	SELECT [value] FROM [dbo].[SplitStringByDelimiter] (@shares, N';')
	--	/*Открываем курсор*/
	--	OPEN @CURSOR
	--	/*Выбираем первую строку*/
	--	FETCH NEXT FROM @CURSOR INTO @CURRENTSHARE
	--	/*Выполняем в цикле перебор строк*/
	--	WHILE @@FETCH_STATUS = 0
	--	BEGIN
	--		IF NOT EXISTS (
	--						SELECT *
	--						FROM [dbo].[mark_Share]
	--						WHERE ([RefShareId] = @CURRENTSHARE OR [ShareId] = @CURRENTSHARE) AND [UserId] = @userId)
	--		BEGIN
	--			DECLARE @shareId nvarchar(32)

	--			SET @shareId = CAST([dbo].[RandString](32) AS NVARCHAR(32))
	--			WHILE EXISTS (SELECT * FROM [dbo].[mark_Share] WHERE [ShareId] = @shareId)
	--			BEGIN
	--				SET @shareId = CAST([dbo].[RandString](32) AS NVARCHAR(32))
	--			END

	--			INSERT INTO [dbo].[mark_Share] ([ShareId],[UserId],[Title],[IsUserTitle],[RefShareId])
	--			SELECT @shareId,@userId,[Title],[IsUserTitle],[ShareId]
	--			FROM [dbo].[mark_Share]
	--			WHERE [ShareId] = @CURRENTSHARE

	--			INSERT INTO [dbo].[mark_Note] ([TextId],[Value],[Title],[UserId],[IsUserTitle],[IsDeleted],[RefNoteId])
	--			SELECT [TextId],[Value],[Title],@userId,[IsUserTitle],0,[NoteId]
	--			FROM [dbo].[mark_Note]
	--			WHERE [NoteId] IN (
	--													SELECT DISTINCT [NoteId]
	--													FROM [dbo].[mark_Share_Text]
	--													WHERE [ShareId] = @CURRENTSHARE 
	--																AND [NoteId] IS NOT NULL 
	--																AND [NoteId] NOT IN (SELECT [RefNoteId] FROM [dbo].[mark_Note] WHERE [UserId] = @userId)
	--												) 

	--			INSERT INTO [dbo].[mark_Share_Text] ([ShareId],[TextId],[NoteId])
	--			SELECT @shareId,[TextId],[NoteId]
	--			FROM [dbo].[mark_Share_Text]
	--			WHERE [ShareId] = @CURRENTSHARE
	--		END
	--		/*Выбираем следующую строку*/
	--		FETCH NEXT FROM @CURSOR INTO @CURRENTSHARE
	--	END
	--	CLOSE @CURSOR		
	--END

	--;WITH chapterTree AS (
	--	SELECT	[ChapterId] = ch.[ChapterId] 
	--					, [ParentChapterId] = ch.[ParentChapterId]
	--	FROM [dbo].[text_Chapter] ch
	--	WHERE ch.[ChapterId] = @chapterId AND ch.[ParentChapterId] IS NOT NULL
	--	UNION ALL
	--	SELECT	ch.[ChapterId]
	--					, ch.[ParentChapterId]
	--	FROM chapterTree t 
	--	INNER JOIN [dbo].[text_Chapter] ch ON ch.[ChapterId] = t.[ParentChapterId]
	--	WHERE ch.[ParentChapterId] IS NOT NULL
	--)

	--SELECT DISTINCT t.[TextId]
	--				,t.[ChapterId]
	--				,t.[Value]
	--				,t.[Order]
	--				,t.[Tag]
	--				,CAST(CASE WHEN t.[IsPublic] = 1 
	--							THEN 1
	--							ELSE	CASE WHEN s.[TextId] IS NOT NULL
	--											THEN 1
	--											ELSE 0
	--										END
	--							END 
	--				 AS BIT) AS [IsPublic]
	--FROM [dbo].[text_Text] AS t
	--LEFT OUTER JOIN (
	--	SELECT st.[TextId]
	--	FROM [dbo].[mark_Share_Text] AS st
	--	INNER JOIN [dbo].[mark_Share] AS s ON s.[ShareId] = st.[ShareId]
	--	WHERE s.[UserId] = @userId
	--	UNION ALL
	--	SELECT st.[TextId]
	--	FROM [dbo].[mark_Share_Text] AS st
	--	WHERE @copyShare = 0 AND st.[ShareId] IN (SELECT [value] FROM [dbo].[SplitStringByDelimiter] (@shares, N';'))
	--) AS s ON t.[TextId] = s.[TextId]
	--WHERE t.[ChapterId] IN (SELECT [ChapterId] FROM chapterTree)

	--------------------- CHAPTER TEXT ------------------------

	--IF @userId IS NULL
	--	RETURN

	--------------------- MARKS ------------------------
	--------------------- NOTES ------------------------

	--SELECT [NoteId]
	--			,[TextId]
	--			,[Value]
	--			,[Title]
	--FROM [dbo].[mark_Note]
	--WHERE [UserId] = @userId AND [IsDeleted] = 0

	--------------------- MARKS ------------------------

	--------------------- BOOKMARKS ------------------------

	--SELECT [BookmarkId]
 --     ,[TextId]
 --     ,[Title]
 -- FROM [dbo].[mark_Bookmark]
	--WHERE [UserId] = @userId

	--------------------- BOOKMARKS ------------------------
	--------------------- SHARE ------------------------

	--SELECT	/*st.[ShareTextId]
	--			,	*/st.[ShareId]
	--			,	st.[TextId]
	--			,	st.[NoteId]
	--			,	t.[Order]
 -- FROM [dbo].[mark_Share_Text] AS st
	--INNER JOIN [dbo].[text_Text] AS t ON t.[TextId] = st.[TextId]
	--WHERE st.[ShareId] IN (
	--		SELECT [ShareId]
	--		FROM [dbo].[mark_Share]
	--		WHERE [UserId] = @userId
	--)

	--SELECT	[ShareId]
	--			--,	[UserId]
	--			,	[Title]
	--			--,	[IsUserTitle]
 -- FROM [dbo].[mark_Share]
	--WHERE [UserId] = @userId

	--------------------- SHARE ------------------------

	--DECLARE @markTextId TABLE
	--(
	--	TextId BIGINT
	--	,	ChapterId UNIQUEIDENTIFIER
	--	,	BookId UNIQUEIDENTIFIER
	--)

	--INSERT INTO @markTextId ([TextId], [ChapterId], [BookId])
	--SELECT DISTINCT t.[TextId], t.[ChapterId], ch.[BookId]
	--FROM [dbo].[mark_Note] AS m
	--INNER JOIN [dbo].[text_Text] AS t ON t.TextId = m.TextId
	--INNER JOIN [dbo].[text_Chapter] AS ch ON t.ChapterId = ch.ChapterId
	--WHERE [UserId] = @userId
	--UNION
	--SELECT DISTINCT t.[TextId], t.[ChapterId], ch.[BookId]
	--FROM [dbo].[mark_Bookmark] AS m
	--INNER JOIN [dbo].[text_Text] AS t ON t.TextId = m.TextId
	--INNER JOIN [dbo].[text_Chapter] AS ch ON t.ChapterId = ch.ChapterId
	--WHERE [UserId] = @userId
	--UNION
	--SELECT DISTINCT t.[TextId], t.[ChapterId], ch.[BookId]
 -- FROM [dbo].[mark_Share_Text] AS st
	--INNER JOIN [dbo].[mark_Share] AS s ON s.ShareId = st.ShareId
	--INNER JOIN [dbo].[text_Text] AS t ON t.TextId = st.TextId
	--INNER JOIN [dbo].[text_Chapter] AS ch ON t.ChapterId = ch.ChapterId
	--WHERE s.[UserId] = @userId

	----SELECT [ChapterId]
	----			,[ParentChapterId]
	----			,[BookId]
	----			,[HashTag]
	----			,[Order]
	----			,[Title]
	----			,[Hint]
	----FROM [dbo].[text_Chapter]
	----WHERE [IsPublished] = 1 AND [BookId] IN (
	----	SELECT DISTINCT [BookId]
	----	FROM @markTextId
	----)
	--SELECT GETUTCDATE()


	--SELECT DISTINCT t.[TextId], t.[ChapterId]
	--FROM @markTextId AS t
	--INNER JOIN [text_Chapter] AS ch ON ch.[ChapterId] = t.[ChapterId]
	--WHERE ch.[BookId] = @bookId


	--------------------- MARKS ------------------------



END 